package day16;

public class learn2 implements Runnable{
    public static void main(String[] args) {
        //实例化线程对象
        learn2 learn2=new learn2();
        Thread thread=new Thread(learn2,"朱玉");
        Thread thread1=new Thread(learn2,"玉竹");
        //启动线程
        thread.start();
        thread1.start();
    }
    int learn2=500;
    int number=0;
    private boolean f=true;
    //重写
    @Override
    public void run() {
        while(f){
            piao();
        }
        //线程枷锁
        //票的业务
        }
    public synchronized  void piao(){
        learn2--;
        number++;
        if(learn2<0){
            f=false;
            return;
        }
        try{
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"抢了第"+number+"号票，还剩:"+learn2+"张");
    }
}
